import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()
root.title('Tree Test')


tree = ttk.Treeview(root)
# tree['columns'] = ('select')
tree.pack()

model_id_dict = {
    'DGS-1210-28ME-B1' : '',
    'DGS-1210-28PME-B1' : '',
    'DGS-1210-28MPME-B1' : ''
}

model_state_dict = {
    'DGS-1210-28ME-B1' : False,
    'DGS-1210-28PME-B1' : False,
    'DGS-1210-28MPME-B1' : False
}

hex_id_dict = {
    'DGS-1210-28ME-B1' : {'1.hex' : '', '2.hex' : '', '3.hex' : ''},
    'DGS-1210-28PME-B1' : {'1.hex' : '', '2.hex' : '', '3.hex' : ''},
    'DGS-1210-28MPME-B1' : {'1.hex' : '', '2.hex' : '', '3.hex' : ''}
}

hex_state_dict = {
    'DGS-1210-28ME-B1' : {'1.hex' : False, '2.hex' : False, '3.hex' : False},
    'DGS-1210-28PME-B1' : {'1.hex' : False, '2.hex' : False, '3.hex' : False},
    'DGS-1210-28MPME-B1' : {'1.hex' : False, '2.hex' : False, '3.hex' : False}
}

model_arr = ['DGS-1210-28ME-B1', 'DGS-1210-28PME-B1', 'DGS-1210-28MPME-B1']

hex_arr = [
    ['1.hex', '2.hex', '3.hex'],
    ['1.hex', '2.hex', '3.hex'],
    ['1.hex', '2.hex', '3.hex']
]

def getModelNameFromTreeId(TreeId:str):
    for (key, val) in model_id_dict.items():
        if val == TreeId:
            return key
    
    return None

def getModelHexFromTreeId(TreeId:str):
    for name in hex_id_dict:
        for (hex,val) in hex_id_dict[name].items():
            if val == TreeId:
                return (name, hex)
    return (None, None)

def isModelSelected(model_name:str):
    return model_state_dict[model_name]

def isHexSelected(model_name:str, hex_name:str):
    return hex_state_dict[model_name][hex_name]

def isModelNode(TreeId:str):
    return getModelNameFromTreeId(TreeId) is not None

def isHexNode(TreeId:str):
    return not isModelNode(TreeId)

def getModelNodeState(model_name:str):
    return model_state_dict[model_name]

def getHexNodeState(model_name:str, hex_name:str):
    return hex_state_dict[model_name][hex_name]

def setModeState(model_name:str, state:bool):
    model_state_dict[model_name] = state

def setHexState(model_name:str, hex_name:str, state:bool):
    hex_state_dict[model_name][hex_name] = state

selected_img = tk.PhotoImage(file='selected.png')
unselected_img = tk.PhotoImage(file='unselected.png')
tree.heading('#0', text='Name')
for idx in range(0, 3):
    model_id_dict[model_arr[idx]] = tree.insert('', 'end', model_arr[idx], text=model_arr[idx], image=unselected_img, tags=('select',))
    for item in hex_arr[idx]:
        id = tree.insert(model_arr[idx], 'end', text=item, image=selected_img, tags=('select',))
        hex_id_dict[model_arr[idx]][item] = id
        # tree.set(id, 'select', 'True')

def itemClicked(val:tk.Event):
    item = tree.focus()
    if isModelNode(item):
        model_name = getModelNameFromTreeId(item)
        model_state = getModelNodeState(model_name)
        setModeState(model_name, not model_state)
        if not model_state is True:
            tree.item(item, image=selected_img)
        else:
            tree.item(item, image=unselected_img)
    else:
        (model_name, hex_name) = getModelHexFromTreeId(item)
        hex_state = getHexNodeState(model_name, hex_name)
        setHexState(model_name, hex_name, not hex_state)
        if not hex_state is True:
            tree.item(item, image=selected_img)
        else:
            tree.item(item, image=unselected_img)

# tree.insert('', 'end', text='button', tags=('ttk', 'simple'))
# tree.tag_configure('ttk', background='yellow')
tree.tag_bind('select', '<3>', itemClicked)  # the item clicked can be found via tree.focus()

root.mainloop()


